set chatmsg(main) 0
hook::add message chatlog 266
proc chatlog {from type body} {
variable chatmsg
	if {[config::optionenable chatlog]} {
		if {[string match "*@conference.*" "$from"]!=0} {
			if {[string range $body 0 2]=="/me"} {
				set text "*[resource_from_jid $from] [string range $body 3 end]"
			} else {
				set text "<[resource_from_jid $from]> $body"
			}
			
			lappend chatmsg([user_from_jid $from]) [XML:decrypt "[clock format [clock seconds] -format \[%H:%M:%S\]] $text"]
			if {[llength $chatmsg([user_from_jid $from])]>200} {
				writechatlog "[server_from_jid $from]" "[node_from_jid $from]" "$chatmsg([user_from_jid $from])"
				lset chatmsg([user_from_jid $from]) ""
				lappend chatmsg([user_from_jid $from]) [XML:decrypt [clock format [clock seconds] -format %d_%B_%Y]]
			}
			return
		}
	}
}

proc writechatlog {serv conf list} {
global hd
	set curd "[file join "$hd" chatlogs htdocs "$serv" "$conf"]"
	if {[file exists "$curd"]} {
		if {![file isdirectory $curd]} {
			putlog "Directory for write log is file"
			return stop
		}
	} else {
		file mkdir $curd
	}
	for {set x 0} {$x<10000} {incr x} {
		if {![file exists "$curd/$x.log"]} {
			set fileq [open "$curd/$x.log" w]
			set ll [llength $list]
			for {set c 0} {$c<$ll} {incr c} {
				puts $fileq "[lindex $list $c]"
			}
			close $fileq
			break
		}
	}
}